package com.gxuwz.borrowprovider.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxuwz.borrowprovider.mapper.BorrowMapper;
import com.gxuwz.commonmodule.entity.BorrowRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BorrowController {
    @Autowired
    private BorrowMapper borrowMapper;

    /**
     * 借阅书籍
     * @param bookID
     */
//    @GetMapping("/borrow/{bookID}")
//    public void borrow(@PathVariable("bookID") String bookID){
//        Book book = bookMapper.findByID(bookID);
//        if (book != null){
//            if (book.getCounts() > 0){
//                book.setCounts(book.getCounts() - 1);
//                System.out.println("借书成功");
//                bookMapper.update(book);
//            }
//        }else {
//            System.out.println("书籍为空");
//        }
//    }

    /**
     * 查询所有借阅记录信息
     * @param borrowRecord
     * @return
     */
    @GetMapping("/list")
    public List<BorrowRecord> list(@RequestBody BorrowRecord borrowRecord){
        return borrowMapper.query(borrowRecord);
    }

    /**
     * 通过ID查找借阅记录信息
     * @param borrowID
     * @return
     */
    @GetMapping("/get/{borrowID}")
    public BorrowRecord getBorrowRecord(@PathVariable("borrowID") Integer borrowID){
        return borrowMapper.findByID(borrowID);
    }


    /**
     * 添加借阅记录信息
     * @param borrowRecord
     */
    @PostMapping("/add")
    public void addBorrowRecord( @RequestBody BorrowRecord borrowRecord) {
        borrowMapper.insert(borrowRecord);
    }

    /**
     * 通过ID删除借阅记录信息
     * @param borrowID
     */
    @DeleteMapping("/delete/{borrowID}")
    public void deleteBorrowRecord(@PathVariable("borrowID") Integer borrowID){
        borrowMapper.delete(borrowID);
    }

    /**
     * 更新借阅记录信息
     * @param borrowRecord
     */
    @PutMapping("/update")
    public void editBorrowRecord(@RequestBody BorrowRecord borrowRecord){
        borrowMapper.update(borrowRecord);
    }

    /**
     * 分页（不带条件）
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public PageInfo findPage(@RequestParam(value = "pageIndex") int pageIndex, @RequestParam(value = "pageSize") int pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        List<BorrowRecord> borrowRecords = borrowMapper.findPage(null);
        PageInfo<BorrowRecord> page = new PageInfo<>(borrowRecords);
        return page;
    }


}
